﻿<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('NCVoucherProject:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="日期">
                <a-date-picker style="width: 100%" placeholder="请选择" v-model="queryParam.billdateDate"
                  @change="onChangebilldate" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" :loading="btnLoading" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button type="primary" :loading="btnLoading" @click="DownLoadProject">数据下载</a-button>
                <a-button type="primary" :loading="btnLoading" @click="UploadProject">数据上传</a-button>

              </span>
            </a-col>

          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table ref="table" 
      :columns="columns" 
      :data="loadData" 
      :alert="true" 
      :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" 
        size="small"
        :scroll="{ x: 1500, y: 600 }" :bordered="true">
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('NCVoucherProject:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('NCVoucherProject:edit') & hasPerm('NCVoucherProject:delete')" />
          <a-popconfirm v-if="hasPerm('NCVoucherProject:delete')" placement="topRight" title="确认删除？"
            @confirm="() => NCVoucherProjectDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
import { STable } from '@/components'
import moment from 'moment'
import { NCVoucherProjectPage, NCVoucherProjectDelete, DownloadHISProject, UploadHISProject } from '@/api/modular/main/NCVoucherProjectManage'

import editForm from './editForm.vue'
export default {
  components: {
    STable,
    editForm
  },
  data() {
    return {
      btnLoading: false,
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {},
      columns: [
        {
          title: 'ID',
          dataIndex: 'billid',
          align: "center"

          , ellipsis: true
          , width: 100
        },
        {
          title: '日期',
          align: 'center',
          dataIndex: 'billdate'
          , customRender: this.formatDate
          , width: 100
        },
        {
          title: '公司',
          align: 'center',
          dataIndex: 'pk_corp'
          , align: "left"
          , width: 100
        },
        {
          title: '序号',
          dataIndex: 'xh'
          , align: "center"
          , width: 100
        },
        {
          title: '项目',
          dataIndex: 'inproject'
          , align: "center"
          , width: 250
          ,customCell: (record) => {
            return { style: { 'text-align': 'left' } };
          }
        },
        {
          title: '本月数(数量或金额)',
          dataIndex: 'mouth_amount'
          , align: "center"
          , width: 180,
          customCell: (record) => {
            return { style: { 'text-align': 'right' } };
          }
        },

        {
          title: '备注',
          dataIndex: 'memo'
          , align: "center"

        }
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return NCVoucherProjectPage(Object.assign(parameter, this.switchingDate())).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created() {
    if (this.hasPerm('NCVoucherProject:edit') || this.hasPerm('NCVoucherProject:delete')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  methods: {
    moment,
    /**
     * 查询参数组装
     */
    switchingDate() {
      const queryParambilldate = this.queryParam.billdateDate
      if (queryParambilldate != null) {
        this.queryParam.billdate = moment(queryParambilldate).format('YYYY-MM-DD')
        if (queryParambilldate.length < 1) {
          delete this.queryParam.billdate
        }
      }
      const obj = JSON.parse(JSON.stringify(this.queryParam))
      return obj
    },
    NCVoucherProjectDelete(record) {
      NCVoucherProjectDelete(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    showMoney(d) {
      return '<div style="align:right">' + d + '</div>'
    },
    formatDate(d) {
      return d ? moment(d).format('YYYY-MM-DD') : ''
    },
    //下载HIS项目数据
    DownLoadProject() {

      let parameter = { billdate: this.queryParam.billdateDate };
      this.btnLoading = true;

      DownloadHISProject(parameter).then((res) => {
        if (res.success) {
          this.$message.success('同步HIS业务凭证成功')
          this.$refs.table.refresh();
        } else {
          this.$message.error('同步失败' + res.message) //
        }
        this.btnLoading = false;
      })
    },
    UploadProject() {
      let parameter = { billdate: this.queryParam.billdateDate };
      this.btnLoading = true;
      UploadHISProject(parameter).then((res) => {
        if (res.success) {
          this.$message.success('上传HIS指标成功')
        } else {
          this.$message.error('上传HIS指标失败' + res.message) //
        }
        this.btnLoading = false;
      })
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    onChangebilldate(date, dateString) {
      this.billdateDateString = dateString
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
</style>
